﻿<?php
class fConnect
{
    public $appID;
    public $appSecret;
    public $canvassPage;
    public $canvassURL;
    public $tabPage;
    public $loginURL;

    public function __construct()
    {
        $settings          = parse_ini_file('settings.ini', true);
        $this->appID       = $settings['app']['id'];
        $this->appSecret   = $settings['app']['secret'];
        $this->canvassPage = $settings['canvass']['page'];
        $this->canvassURL  = $settings['canvass']['url'];
        $this->tabPage     = $settings['tab']['page'];
        
        $this->loginURL    = "http://www.facebook.com/dialog/oauth?";
        $this->loginURL   .= http_build_query(array(
                                'client_id'    => $settings['app']['id'],
                                'redirect_uri' => $this->redirectURI(),
                                'scope'        => $settings['app']['scope']
                             ));
    }
    
    public function isAuthorized()
    {
        $data = $this->signedRequest();
        $code = $this->code();
        
        if (!empty($data) && isset($data['user_id'])) {
            return true;
        } else if (!empty($code)) {
            return true;
        } else {
            return false;
        }
    }
    
    public function signedRequest()
    {
        if (isset($_REQUEST['signed_request'])) {
            list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);

            $sig     = base64_decode(strtr($encoded_sig, '-_', '+/'));
            $sigUser = json_decode(base64_decode(strtr($payload, '-_', '+/'), true), true);
            
            return $sigUser;
        } else {
            return false;
        }
    }
    
    public function code()
    {
        if (isset($_REQUEST['code'])) {
            $tokenURL  = "https://graph.facebook.com/oauth/access_token?";
            $tokenURL .= http_build_query(array(
                'client_id'     => $this->appID,
                'redirect_uri'  => $this->redirectURI(),
                'client_secret' => $this->appSecret,
                'code'          => $_REQUEST['code']
            ));

            $response = @file_get_contents($tokenURL);
            $data     = null;

            parse_str($response, $data);

            return $data;
        } else {
            return false;
        }
    }
    
    public function redirectURI()
    {
        if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
            $this->canvassURL  = str_replace("http", "https", $this->canvassURL);
            $this->canvassPage = str_replace("http", "https", $this->canvassPage);
        }

        if (isset($_SERVER['HTTP_REFERER'])) {
            if ($_SERVER['HTTP_REFERER'] == 'http://static.ak.facebook.com/platform/page_proxy.php?v=4' && !empty($this->tabPage)) {
                return $this->tabPage;
            } else {
                return $this->canvassPage;
            }
        } else {
            return $this->canvassURL;
        }
    }
    
    public function graph($param, $token)
    {
        $graphURL = "https://graph.facebook.com/" . $param . "?access_token=" . $token;
        return json_decode(@file_get_contents($graphURL), true);
    }
    
    public function fql($query, $token)
    {
        $fqlURL  = "https://api.facebook.com/method/fql.query?";
        $fqlURL .= http_build_query(array(
            'query'        => urlencode($query),
            'access_token' => $token,
            'format'       => 'JSON'
        ));

        return json_decode(@file_get_contents($fqlURL), true);
    }
    
    public function post($params, $token)
    {
        $params['access_token'] = $token;

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/me/feed");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 6);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

        $data = curl_exec($ch);

        curl_close($ch);

        return $data;
    }
    
    public function upload($fbid, $image, $token)
    {
        $fieldString['image'] = '@' . $image;

        $ch = curl_init();
        curl_setopt($ch ,CURLOPT_URL, "https://graph.facebook.com/" . $fbid . "/photos?access_token=" . $token);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 6);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString);

        $data = curl_exec($ch);

        curl_close($ch);

        return $data;
    }
}